package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.AlarmEquipmentRecordVo;
import com.moli.iov.admin.model.vo.req.AlarmEquipmentRecordVo.Update;
import com.moli.iov.admin.service.AlarmEquipmentRecordService;
import com.moli.iov.base.enums.AlarmTypeEnum;
import com.moli.iov.base.enums.HandlingStatusEnum;
import com.moli.iov.base.model.dto.req.AlarmEquipmentRecordDto;
import com.moli.iov.base.model.dto.res.AlarmEquipmentRecordResDto;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The type Alarm equipment record controller.
 *
 * 报警管理 > 设备变更报警
 *
 * @author wanghualin
 * @date 2019-09-27 13:21:42
 */
@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmEquipmentRecordController extends BaseController {

  @Autowired
  private AlarmEquipmentRecordService recordService;

  /**
   * 设备变更报警-list
   */
  @PostMapping("/alarmEquipmentRecord/list")
  public RestResponse saveEquipmentRecord(@RequestBody AlarmEquipmentRecordVo vo) {
    log.info("设备变更报警 入参：{}", vo);
    assembleUserInfo(vo);
    AlarmEquipmentRecordDto dto = new AlarmEquipmentRecordDto();
    CommonUtils.copyProperties(vo, dto);
    PageExtend<AlarmEquipmentRecordResDto> recordResDtoPageExtend = recordService
        .listEquipmentRecord(dto);
    return RestResponse.success(recordResDtoPageExtend);
  }

  /**
   * 设备变更报警-update
   */
  @PostMapping("/alarmEquipmentRecord/update")
  public RestResponse updateEquipmentRecord(@RequestBody @Validated({
      Update.class}) AlarmEquipmentRecordVo vo) {
    log.info("设备变更报警 修改 入参：{}", vo);
    assembleUserInfo(vo);
    AlarmEquipmentRecordDto dto = new AlarmEquipmentRecordDto();
    CommonUtils.copyProperties(vo, dto);
    return recordService
        .updateAlarmEquipmentRecord(dto);
  }

  /**
   * 设备变更报警-导出
   */
  @PostMapping("/alarmEquipmentRecord/export")
  public RestResponse saveEquipmentExport(@RequestBody AlarmEquipmentRecordVo vo,
      HttpServletRequest request,
      HttpServletResponse response) {
    log.info("设备变更导出 入参：{}", vo);
    assembleUserInfo(vo);
    AlarmEquipmentRecordDto dto = new AlarmEquipmentRecordDto();
    CommonUtils.copyProperties(vo, dto);
    dto.setPageNum((long) 1);
    dto.setPageSize(Constants.EXPORT_PAGE_SIZE);
    PageExtend<AlarmEquipmentRecordResDto> export = recordService
        .listEquipmentRecord(dto);
    if (export != null && !CollectionUtils.isEmpty(export.getRecords())) {
      for (AlarmEquipmentRecordResDto record : export.getRecords()) {
        record.setAlarmTypeStr(AlarmTypeEnum.getCodeName(record.getAlarmType()));
        record.setHandlingStatusStr(HandlingStatusEnum.getCodeName(record.getAlarmType()));
      }
    }
    String[] propNames = {"vin", "license", "vehicleModel", "alarmTypeStr",
        "operationRemark", "gmtCreate", "handlingStatusStr"};
    String[] headStrs = {"车架号", "车牌号", "车型", "报警类型", "报警内容", "报警时间", "报警状态"};
    String title = "设备变更报警";
    try {
      ExcelUtil.exportDataExcel(request, response,
          CollectionUtils.isEmpty(export.getRecords()) ? new ArrayList() : export.getRecords(),
          propNames, headStrs, title);
      return RestResponse.success();
    } catch (Exception e) {
      e.printStackTrace();
      return RestResponse.failure("导出异常");
    }
  }
}
